package com.k.kc.util.list.demo1;

import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 集合操作
 * @author kivil
 */
public class ListTest {
    public static void main(String[] args) {
        //test();
//        test11();
//        System.out.println("==========");
        test1();
//        List<String> list1 = Arrays.asList("A","B","C","D");
//        List<String> list2 = Arrays.asList("A","C","E");
//        System.out.println("删除的："+CollectionUtils.subtract(list1,list2));
//        System.out.println("新增的："+CollectionUtils.subtract(list2,list1));
//
//        Set<String> set = new HashSet();
//        set.add("a");
//        set.add("b");
//        System.out.println(set);
//
//        String abc = null;
//        Optional<String> opt = Optional.ofNullable(abc);
//        opt.ifPresent(a-> System.out.println("value is not null"));
//        opt.orElse("");
//        System.out.println(opt.get());
    }
    private static void test(){
        List<Person> list1 = new ArrayList<>();
        Person p1 = new Person("小",22);
        Person p2 = new Person("中",23);
        Person p13 = new Person("x",23);
        list1.add(p1);
        list1.add(p2);
        list1.add(p13);

        List<Person> list2 = new ArrayList<>();
        Person p3 = new Person("中",23);
        Person p4 = new Person("大",30);
        Person p5 = new Person("小",22);
        list2.add(p3);
        list2.add(p4);
        list2.add(p5);
        System.out.println("差集 list1-list2："+ CollectionUtils.subtract(list1,list2));
        System.out.println("差集 list2-list1："+ CollectionUtils.subtract(list2,list1));
        System.out.println("并集：" + CollectionUtils.union(list1,list2));
        System.out.println("交集：" + CollectionUtils.intersection(list1,list2));
        System.out.println("交集的补集："+CollectionUtils.disjunction(list1,list2));
    }
    private static void test11(){
        List<Person> dbList = new ArrayList<>();
        Person p1 = new Person("小",22);
        Person p2 = new Person("中",23);
        Person p13 = new Person("x",23);
        Person p14 = new Person("y",23);
        //dbList.add(p1);
        //dbList.add(p2);
        dbList.add(p13);
        dbList.add(p14);

        List<Person> inputList = new ArrayList<>();
        Person p3 = new Person("中",23);
        Person p4 = new Person("大",30);
        Person p5 = new Person("小",22);
        Person p6 = new Person("z",22);
        //inputList.add(p3);
        inputList.add(p4);
        //inputList.add(p5);
        inputList.add(p6);

        Collection<Person> delList = null;
        Collection<Person> addList = null;
        if(!CollectionUtils.isEmpty(dbList) && CollectionUtils.isEmpty(inputList)){
            // all delete
            delList = dbList;
        }else if(CollectionUtils.isEmpty(dbList) && !CollectionUtils.isEmpty(inputList)){
            // all insert
            addList = inputList;
        }else if(!CollectionUtils.isEmpty(dbList) && !CollectionUtils.isEmpty(inputList)){
            // delete
            delList = CollectionUtils.subtract(dbList,inputList);
            // insert
            addList = CollectionUtils.subtract(inputList,dbList);

            Collection<Person> jj = CollectionUtils.intersection(dbList,inputList);
            Collection<Person> db_jj_del = CollectionUtils.subtract(dbList,jj);
            Collection<Person> input_jj_add = CollectionUtils.subtract(inputList,jj);
            System.out.println("交集：" + CollectionUtils.intersection(dbList,inputList));
            System.out.println("db-交集=del：" + db_jj_del);
            System.out.println("input-交集=add：" + input_jj_add);
        }else{
            // ignore
            System.out.println("ignore");
        }

        if(CollectionUtils.isNotEmpty(delList)){
            System.out.println("delete: "+delList);
        }
        if(CollectionUtils.isNotEmpty(addList)){
            System.out.println("add: "+addList);
        }
    }

    private static void test1(){
        List<String> list1 = Arrays.asList("a","b","c");
        List<String> list2 = Arrays.asList("b","c","d");

        List<String> list1SubList2 = list1.stream().filter(a->!list2.contains(a)).collect(Collectors.toList());
        List<String> list2SubList1 = list2.stream().filter(a->!list1.contains(a)).collect(Collectors.toList());
        List<String> listJiaoJi = list1.stream().filter(list2::contains).collect(Collectors.toList());
        List<String> listUnion = new ArrayList<>();listUnion.addAll(list1);listUnion.addAll(list2);
        List<String> listBuji = new ArrayList<>();listBuji.addAll(list1SubList2);listBuji.addAll(list2SubList1);

        System.out.println("差集：list1-list2："+list1SubList2);
        System.out.println("差集：list2-list1："+list2SubList1);
        System.out.println("交集："+ listJiaoJi);
        System.out.println("并集："+ listUnion.stream().distinct().collect(Collectors.toList()));
        System.out.println("交集的补集："+listBuji);
    }
}
